<?php

namespace app\api\controller\plus\bargain;

use app\api\controller\Controller;
use app\api\model\plus\bargain\Product as BargainProductModel;
use app\api\model\plus\bargain\Task as TaskModel;
use app\api\model\plus\bargain\Product as TaskProductModel;
use app\api\model\plus\bargain\ProductSku as ProductSkuModel;
use app\shop\model\settings\Setting as SettingModel;

/**
 * 助力任务模型
 */
class Task extends Controller
{

    /**
     *创建助力任务
     * add
     * @param $bargain_activity_id
     * @param $bargain_product_id
     * @param $bargain_product_sku_id
     * @param $product_sku_id
     * \think\response\Json
     * @return \think\response\Json
     * @throws \app\common\exception\BaseException
     * @throws \app\common\exception\BaseException
     * @Author kevin
     * @date 2024/4/18 10:04
     */
    public function add($bargain_activity_id, $bargain_product_id, $bargain_product_sku_id, $product_sku_id)
    {
        // 用户信息
        $user = $this->getUser();
        // 创建助力任务
        $model = new TaskModel;
        if (!$model->add($user['user_id'], $bargain_activity_id, $bargain_product_id, $bargain_product_sku_id, $product_sku_id)) {
            return $this->renderError($model->getError() ?: '任务创建失败');
        }
        //增加参与人数
        (new BargainProductModel)->where('bargain_product_id', $bargain_product_id)->inc('join_num')->update();
        return $this->renderSuccess('任务创建成功', [
            'bargain_task_id' => $model['bargain_task_id']
        ]);
    }

    /**
     *获取助力任务详情
     * detail
     * @param $bargain_task_id
     * @param $url
     * \think\response\Json
     * @return \think\response\Json
     * @throws \app\common\exception\BaseException
     * @throws \app\common\exception\BaseException
     * @Author kevin
     * @date 2024/4/18 10:03
     */
    public function detail($bargain_task_id, $url = '')
    {
        $detail = (new TaskModel)->getTaskDetail($bargain_task_id, $this->getUser(false));
        //分享
        $share = $this->getShareParams($url, "发现了一个好物，快来帮我助力", $detail['task']['product_name'], '/pages/plus/bargain/haggle/haggle', $detail['product']['product']['image'][0]['file_path']);
        return $this->renderSuccess('', array_merge($detail, compact('share')));
    }

    /**
     *帮砍一刀
     * cut
     * @param $bargain_task_id
     * \think\response\Json
     * @return \think\response\Json
     * @throws \app\common\exception\BaseException
     * @throws \app\common\exception\BaseException
     * @Author kevin
     * @date 2024/4/18 10:04
     */
    public function cut($bargain_task_id)
    {
        // 助力任务详情
        $model = TaskModel::detail($bargain_task_id);
        // 砍一刀的金额
        $cut_money = $model->getCutMoney();

        //验证是否符合条件帮砍
        $check_cut = $model->checkCut($this->getUser(),$model['bargain_activity_id'],$bargain_task_id);
        if(!$check_cut){
            return $this->renderError('您不符合参与条件或活动已结束');
        }

        $user = $this->getUser();

        if(!$user['mobile']){
            return $this->renderError('您不符合参与条件或活动已结束');
        }

        // 帮砍一刀事件
        $status = $model->helpCut($this->getUser());
        if ($status == true) {
            return $this->renderSuccess('助力成功', compact('cut_money'));
        }
        return $this->renderError($model->getError() ?: '助力失败');
    }

    /**
     *我的助力列表
     * getTaskList
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \app\common\exception\BaseException
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/4/18 11:40
     */
    public function getTaskList(){
        $user = $this->getUser();
        $param = $this->getData();
        $list = (new TaskModel()) -> getList($user['user_id'],$param);
        $bargain_task_ids = [];
        foreach ($list as $key => $value){
            if($value['end_time'] < time() && $value['status'] == 0){ //到期还在进行中的状态设为2
                $list[$key]['status'] = 2;
                $bargain_task_ids[] = $value['bargain_task_id'];
            }

            $product_sku = ProductSkuModel::where('bargain_product_id',$value['bargain_product_id']) -> where('bargain_product_sku_id',$value['bargain_product_sku_id']) -> find();

            if($product_sku){
                $list[$key]['product_stock'] = $product_sku['bargain_stock'];
            }else{
                $list[$key]['product_stock'] = TaskProductModel::where('bargain_product_id',$value['bargain_product_id']) -> value('stock');
            }


        }
        if($bargain_task_ids){
            (new TaskModel()) -> where('bargain_task_id','in',$bargain_task_ids) -> update(['status' => 2]);
        }
        return $this->renderSuccess('我的助力列表', compact('list'));
    }


    /**
     *活动设置
     * getSetting
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/7 10:43
     */
    public function getSetting()
    {
        $vars['values'] = SettingModel::getItem('bargain');
        return $this->renderSuccess('', compact('vars'));
    }

}